﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MemoLib;
using MemoLib.Entity;

namespace UI
{
    public partial class frmMessage : Form
    {
        public frmMessage()
        {
            InitializeComponent();
        }

        private int lastY = 20;

        public bool IsForTodoList = false;

        public void ShowMessage()
        {
            base.Show();
            int i = 0;
            foreach (string message in ExecuteContext.MessageBusket)
            {
                string msg = message;
                List<string> webSites = new List<string>();
                // 拆分提示文本和网址部分
                ContextAndWebsites caws = Utility.SplitContextAndWebsite(message);
                msg = caws.Conterxt;
                string[] tempArry = message.Split(new string[] { CommonData.WEBSITE_SPLIT },
                    StringSplitOptions.RemoveEmptyEntries);

                i++;
                CreateLabelToDisplay(i, msg); // 创建label控件显示提示信息
                // 创建LinkLabel显示网址信息（如果有）
                CreateLinkLabelToDisplay(caws.Website);
                CreateLinkLabelToDisplay(caws.Website2);

                this.lastY += 10;
            }
        }

        /// <summary>
        /// 创建label控件显示提示信息
        /// </summary>
        /// <param name="website"></param>
        private void CreateLinkLabelToDisplay(string website)
        {
            if (string.IsNullOrEmpty(website)) return;

            LinkLabel lblWebsite = new LinkLabel();
            lblWebsite.LinkClicked += new LinkLabelLinkClickedEventHandler(lblWebsite_LinkClicked);
            this.Controls.Add(lblWebsite);
            lblWebsite.Location = new Point(20, this.lastY + CommonData.LOCATION_OFFSET);
            lblWebsite.AutoSize = true;
            lblWebsite.Text = website;
            lblWebsite.Links.Add(0, website.Length, website);
            this.lastY = lblWebsite.Location.Y;
        }

        /// <summary>
        /// 创建LinkLabel显示网址信息（如果有）
        /// </summary>
        /// <param name="i"></param>
        /// <param name="msg"></param>
        private void CreateLabelToDisplay(int i, string msg)
        {
            Label lbl = new Label();
            this.Controls.Add(lbl);
            lbl.Location = new Point(20, this.lastY + CommonData.LOCATION_OFFSET);
            lbl.AutoSize = true;
            lbl.Text = i.ToString() + ". " + msg;
            this.lastY = lbl.Location.Y;
        }

        /// <summary>
        /// 点击超链接，连接在默认的浏览器中打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lblWebsite_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string targetUrl = e.Link.LinkData as string;
            System.Diagnostics.Process.Start(targetUrl);
            // 如果弹出窗体是双击“Todo List”而弹出，那么单击连接后直接关闭。
            if (this.IsForTodoList)
            {
                this.Close();
            }
        }

        /// <summary>
        /// 窗体加载的时候的初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMessage_Load(object sender, EventArgs e)
        {
            this.Size = new Size(this.Size.Width, this.lastY + CommonData.LOCATION_OFFSET + 5);
        }

        /// <summary>
        /// 双击窗体，关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMessage_DoubleClick(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
